package com.south.base.test.arithmetic;

import com.south.base.utils.PalindromeUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;

import static com.south.base.utils.PalindromeUtil.*;

/**
 * 回文算法
 */
@Slf4j
public class Palindrome {

    @Test
    public void palindrome() {
        String source = "babad";
        boolean palindrome = PalindromeUtil.isPalindrome(source);
        log.info("是否是回文：{}", palindrome);
        if (!palindrome) {
            log.info("所能组成最长的回文长度：{}", longestPalindrome(source));
            log.info("最长的回文子序列：{}", longestPalindromeSubseq(source));
            log.info("内部最长的回文：{}", longestInsidePalindrome(source));
        }
    }

    @Test
    public void numPalindromeTest() {
        String source = "aaaa";
        int count = numPalindrome(source);
        log.info("组成回文个数：{}", count);
        Assert.assertEquals(10, count);
        Assert.assertEquals(6, numPalindrome("aaa"));
        Assert.assertEquals(4, numPalindrome("aba"));
    }

    @Test
    public void isPalindrome() {
        Assert.assertTrue(PalindromeUtil.isLetterOrDigitPalindrome("A man, a plan, a canal: Panama"));
        Assert.assertTrue(PalindromeUtil.validPalindrome("abca"));
        Assert.assertTrue(PalindromeUtil.validPalindrome("acbbca"));
        Assert.assertTrue(PalindromeUtil.validPalindrome("acbbcca"));
        Assert.assertTrue(PalindromeUtil.validPalindrome("aba"));
        Assert.assertFalse(PalindromeUtil.validPalindrome("A man, a plan, a canal: Panama"));

        PalindromeUtil.ListNode head = new PalindromeUtil.ListNode(1);
        head.setNext(new PalindromeUtil.ListNode(2));
        Assert.assertFalse(PalindromeUtil.isPalindrome(head));
        PalindromeUtil.ListNode head2 = new PalindromeUtil.ListNode(1);
        PalindromeUtil.ListNode next1 = new PalindromeUtil.ListNode(0);
        next1.setNext(new PalindromeUtil.ListNode(1));
        head2.setNext(next1);
        Assert.assertTrue(PalindromeUtil.isPalindrome(head2));
    }

    /**
     * 回文对
     * 给定一组唯一的单词，
     * 找出所有不同 的索引对(i, j)，使得列表中的两个单词，
     * words[i] + words[j] ，可拼接成回文串。
     */
    @Test
    public void palindromePairs() {
        Assert.assertEquals("[[0, 1], [1, 0], [3, 2], [2, 4]]", PalindromeUtil.palindromePairs(new String[]{"abcd", "dcba", "lls", "s", "sssll"}).toString());
        Assert.assertEquals("[[0, 1], [1, 0]]", PalindromeUtil.palindromePairs(new String[]{"bat", "tab", "cat"}).toString());
    }
}
